package com.qf.fcy.youshare;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import com.qf.fcy.youshare.adapter.CommonFragmentAdapter;
import com.qf.fcy.youshare.fragment.StoreCoreFragment;
import com.qf.fcy.youshare.fragment.StorePlatformFragment;
import com.qf.fcy.youshare.model.Category;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Alex[FR]
 * Date: 2015/2/5
 * Email: alex_fcy@163.com
 */

/**
 * 商城界面的Activit,
 */
public class StoreActivity extends FragmentActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener {

    private ViewPager pager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);

        pager = (ViewPager) findViewById(R.id.pager_store);


        // ActionBar 调整样式
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            // 设置ActionBar TAB导航
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            // 设置返回按钮
            actionBar.setDisplayHomeAsUpEnabled(true);
            // 去除图标
            actionBar.setIcon(new ColorDrawable());
            actionBar.setBackgroundDrawable(new ColorDrawable(0xFFF7784B));
            // 标题居中
            actionBar.setDisplayShowCustomEnabled(true);
            // 取消 ActionBar 标题
            actionBar.setDisplayShowTitleEnabled(false);
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.action_bar_main_title, null);
            TextView title = (TextView) view.findViewById(R.id.action_bar_title);
            title.setText("产品");

            actionBar.setCustomView(
                    view,
                    new ActionBar.LayoutParams(Gravity.CENTER_HORIZONTAL));

            DataStorage dataStorage = DataStorage.getInstance();
            Category firstCat = dataStorage.getCategory("1");
            Category secondCat = dataStorage.getCategory("2");

            if (firstCat != null) {
                String firstCatName = firstCat.getName();
                if (firstCatName == null) {
                    firstCatName = "平台产品";
                }
            }
            if (secondCat != null) {
                String secondCatName = secondCat.getName();
                if (secondCatName == null) {
                    secondCatName = "平台产品";
                }
            }

            // ActionBar 添加 Tab
            actionBar.addTab(actionBar.newTab().setText(firstCat.getName())
                                     .setTag(0).setTabListener(this));
            actionBar.addTab(actionBar.newTab().setText(secondCat.getName())
                                     .setTag(1).setTabListener(this));

            // 对于ActionBar Tab 与 ViewPager 联动，需要提交创建 ViewPager实例
            ArrayList<Fragment> list = new ArrayList<Fragment>();
            // 添加Fragment
            list.add(new StoreCoreFragment());
            list.add(new StorePlatformFragment());
            CommonFragmentAdapter adapter = new CommonFragmentAdapter(
                    getSupportFragmentManager(),
                    list);

            pager.setAdapter(adapter);
            pager.setOnPageChangeListener(this);

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_store, menu);
        return true;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        if (pager != null) {
            ActionBar actionBar = getActionBar();
            if (actionBar != null) {
                int index = actionBar.getSelectedNavigationIndex();
                pager.setCurrentItem(index);
            }
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setSelectedNavigationItem(i);
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}